package com.example.shixun.controller;

import com.example.shixun.pojo.Result;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@RestController
public class UploadController {
    private static String accessKey="v0rIq-sb8e9EenN2qQ-t_foC08ehb5B-X0tjM3-k";// 公钥
    private static String accessSecretKey="Na7M-e-W63BUKrf7aPB5Z8nXXkhGJNh67gFkvzsg"; // 私钥
    private static String bucket="shixun-lys";// 储存空间
    private static String url="http://rwxu0rlu4.hn-bkt.clouddn.com";
    @RequestMapping("/uploadImage")
    public Result ImageUpload(@RequestParam("avatar") MultipartFile imgFile)  {
        System.out.println("上传图片");
        try {
            //1、获取文件上传的流
            byte[] fileBytes = imgFile.getBytes();
            //2、创建日期目录分隔
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            String datePath = dateFormat.format(new Date());

            //3、获取文件名
            String originalFilename = imgFile.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

            //4.构造一个带指定 Region 对象的配置类
            //Region.南(根据自己的对象空间的地址选
            Configuration cfg = new Configuration(Region.huanan());
            UploadManager uploadManager = new UploadManager(cfg);

            //5.获取七牛云提供的 token
            Auth auth = Auth.create(accessKey, accessSecretKey);
            String upToken = auth.uploadToken(bucket);
            uploadManager.put(fileBytes, originalFilename, upToken);

            return Result.success("上传成功，data为url", url + "/" + originalFilename);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Result.fail("上传失败");
    }
    @RequestMapping("/uploadVideo")
    public Result VideoUpload(@RequestParam("video") MultipartFile imgFile) throws IOException {
        System.out.println("上传视频");
        try {
            //1、获取文件上传的流
            byte[] fileBytes = imgFile.getBytes();

            //2、创建日期目录分隔
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
            String datePath = dateFormat.format(new Date());

            //3、获取文件名
            String originalFilename = imgFile.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

            String filename = datePath+"/"+ UUID.randomUUID().toString().replace("-", "")+ suffix;
            //4.构造一个带指定 Region 对象的配置类
            //Region.南(根据自己的对象空间的地址选
            Configuration cfg = new Configuration(Region.huanan());
            UploadManager uploadManager = new UploadManager(cfg);

            //5.获取七牛云提供的 token
            Auth auth = Auth.create(accessKey, accessSecretKey);
            String upToken = auth.uploadToken(bucket);
            uploadManager.put(fileBytes, filename, upToken);

            return Result.success("上传成功", url + "/" + filename);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return Result.fail("上传失败");
    }

}
